package com.maslyk.invitation.jsfcontroller;

import java.util.List;

import javax.faces.event.ActionEvent;

import org.apache.myfaces.trinidad.component.UIXIterator;
import org.springframework.beans.factory.annotation.Autowired;

import com.maslyk.invitation.BeanService;
import com.maslyk.invitation.entitybean.PriceParameter;
import com.maslyk.invitation.enums.LocalNames;
import com.maslyk.invitation.exception.LogicalException;
import com.maslyk.invitation.service.PersistenceService;

public class PriceParameterController
{
    private UIXIterator priceParameterIterator;
    
	@Autowired
	private PersistenceService persistenceService;
    
    public PriceParameterController()
    {
    }
	
    public void addEmptyPriceParameter(ActionEvent event)
    {
        PriceParameter param = new PriceParameter();
    	BeanService.putSessionScopeBean(PriceParameter.getBeanName(), param);
    }
    
    public void removePriceParameter(ActionEvent event) throws Exception
    {
    	PriceParameter param = (PriceParameter)priceParameterIterator.getRowData();
    	
    	if(!param.getFeeList().isEmpty())
    		throw new LogicalException(LocalNames.LOGICAL_ERROR_HEADER, "Istnieja zarejestrowane zamowienia na te prowizje");
    	
    	persistenceService.removePriceParameter(param);
    	BeanService.putSessionScopeBean(PriceParameter.getBeanName(), null);
    }
    
    public void editPriceParameter(ActionEvent event) throws Exception
    {
    	PriceParameter param = (PriceParameter)priceParameterIterator.getRowData();
    	BeanService.putSessionScopeBean(PriceParameter.getBeanName(), param);
    }
    
    public String savePriceParameter() throws Exception
    {
    	PriceParameter param = (PriceParameter) BeanService.getSessionScopeBean(PriceParameter.getBeanName());
    	persistenceService.savePriceParameter(param);
    	BeanService.putSessionScopeBean(PriceParameter.getBeanName(), null);
    	
    	return null;
    }

	public UIXIterator getPriceParameterIterator()
	{
		return priceParameterIterator;
	}

	public void setPriceParameterIterator(UIXIterator priceParameterIterator)
	{
		this.priceParameterIterator = priceParameterIterator;
	}
	
	public List<PriceParameter> getPriceParameters()
	{
		return persistenceService.getPriceParameterList();
	}
}
